package com.jeeplus.common.utils.sms;

import com.jeeplus.common.utils.SpringContextHolder;
import com.jeeplus.modules.sys.entity.SysConfig;
import com.jeeplus.modules.sys.service.SysConfigService;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;

import com.tencentcloudapi.sms.v20190711.SmsClient;

import com.tencentcloudapi.sms.v20190711.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20190711.models.SendSmsResponse;
import com.tencentcloudapi.sms.v20190711.models.SendStatus;
import org.springframework.beans.factory.annotation.Autowired;


public class SmsUtilsByTencent
{
    @Autowired
    SysConfigService sysConfigService;

    /**
     * 发送短信验证码 注册
     * @param phoneNumber 国内手机号(此方法会在号码前添加+86)
     * @param verificationCode 6位数字验证码
     * @return SendStatus.getCode()=="Ok"，表示发送成功
     * @throws TencentCloudSDKException
     */
    public static SendStatus sendSmsVerificationCode(String phoneNumber, String verificationCode) throws TencentCloudSDKException {
        SysConfig config = SpringContextHolder.getBean(SysConfigService.class).get("1");
        // TODO 需要修改到配置文件
        Credential cred = new Credential(config.getTencentApiSecretId(), config.getTencentApiSecretKey());
        HttpProfile httpProfile = new HttpProfile();
        httpProfile.setEndpoint("sms.tencentcloudapi.com");

        ClientProfile clientProfile = new ClientProfile();
        clientProfile.setHttpProfile(httpProfile);
        SmsClient client = new SmsClient(cred, "", clientProfile);
        SendSmsRequest req = new SendSmsRequest();
        req.setPhoneNumberSet(new String[]{"+86"+phoneNumber});
        //验证码模板ID
        req.setTemplateID(config.getTencentSmsVerificationCodeTemplateID());// TODO 需要修改到配置文件
        //审核通过的签名
        req.setSign(config.getTencentSmsSign());// TODO 需要修改到配置文件
        req.setTemplateParamSet(new String[]{verificationCode});
        //短信应用ID
        req.setSmsSdkAppid(config.getTencentSmsSdkAppid());//TODO 需要修改到配置文件
        SendSmsResponse resp = client.SendSms(req);
        System.out.println(SendSmsRequest.toJsonString(resp));
        return resp.getSendStatusSet()[0];
    }

    /**
     * 发送短信验证码 修改
     * @param phoneNumber 国内手机号(此方法会在号码前添加+86)
     * @param verificationCode 6位数字验证码
     * @return SendStatus.getCode()=="Ok"，表示发送成功
     * @throws TencentCloudSDKException
     */
    public static SendStatus sendSmsVerificationUpdate(String phoneNumber, String verificationCode) throws TencentCloudSDKException {
        SysConfig config = SpringContextHolder.getBean(SysConfigService.class).get("3");
        // TODO 需要修改到配置文件
        Credential cred = new Credential(config.getTencentApiSecretId(), config.getTencentApiSecretKey());

        HttpProfile httpProfile = new HttpProfile();
        httpProfile.setEndpoint("sms.tencentcloudapi.com");

        ClientProfile clientProfile = new ClientProfile();
        clientProfile.setHttpProfile(httpProfile);
        SmsClient client = new SmsClient(cred, "", clientProfile);
        SendSmsRequest req = new SendSmsRequest();
        req.setPhoneNumberSet(new String[]{"+86"+phoneNumber});
        //验证码模板ID
        req.setTemplateID(config.getTencentSmsVerificationCodeTemplateID());// TODO 需要修改到配置文件
        //审核通过的签名
        req.setSign(config.getTencentSmsSign());// TODO 需要修改到配置文件
        req.setTemplateParamSet(new String[]{verificationCode});
        //短信应用ID
        req.setSmsSdkAppid(config.getTencentSmsSdkAppid());//TODO 需要修改到配置文件
        SendSmsResponse resp = client.SendSms(req);
        System.out.println(SendSmsRequest.toJsonString(resp));
        return resp.getSendStatusSet()[0];
    }

    /**
     * 办件修改
     * @param phoneNumber 国内手机号(此方法会在号码前添加+86)
     * @param typeName 业务名称
     * @return SendStatus.getCode()=="Ok"，表示发送成功
     * @throws TencentCloudSDKException
     */
    public static SendStatus SendUpdate(String phoneNumber, String typeName) throws TencentCloudSDKException {
        SysConfig config = SpringContextHolder.getBean(SysConfigService.class).get("2");
        // TODO 需要修改到配置文件
        Credential cred = new Credential(config.getTencentApiSecretId(), config.getTencentApiSecretKey());

        HttpProfile httpProfile = new HttpProfile();
        httpProfile.setEndpoint("sms.tencentcloudapi.com");

        ClientProfile clientProfile = new ClientProfile();
        clientProfile.setHttpProfile(httpProfile);
        SmsClient client = new SmsClient(cred, "", clientProfile);
        SendSmsRequest req = new SendSmsRequest();
        req.setPhoneNumberSet(new String[]{"+86"+phoneNumber});
        //验证码模板ID
        req.setTemplateID(config.getTencentSmsVerificationCodeTemplateID());// TODO 需要修改到配置文件
        //审核通过的签名
        req.setSign(config.getTencentSmsSign());// TODO 需要修改到配置文件
        req.setTemplateParamSet(new String[]{typeName});
        //短信应用ID
        req.setSmsSdkAppid(config.getTencentSmsSdkAppid());//TODO 需要修改到配置文件
        SendSmsResponse resp = client.SendSms(req);
        System.out.println(SendSmsRequest.toJsonString(resp));
        return resp.getSendStatusSet()[0];
    }

    /**
     * 办件修改
     * @param phoneNumber 国内手机号(此方法会在号码前添加+86)
     * @param typeName 业务名称
     * @return SendStatus.getCode()=="Ok"，表示发送成功
     * @throws TencentCloudSDKException
     */
    public static SendStatus SendResultEnd(String phoneNumber, String typeName) throws TencentCloudSDKException {
        SysConfig config = SpringContextHolder.getBean(SysConfigService.class).get("4");
        // TODO 需要修改到配置文件
        Credential cred = new Credential(config.getTencentApiSecretId(), config.getTencentApiSecretKey());

        HttpProfile httpProfile = new HttpProfile();
        httpProfile.setEndpoint("sms.tencentcloudapi.com");

        ClientProfile clientProfile = new ClientProfile();
        clientProfile.setHttpProfile(httpProfile);
        SmsClient client = new SmsClient(cred, "", clientProfile);
        SendSmsRequest req = new SendSmsRequest();
        req.setPhoneNumberSet(new String[]{"+86"+phoneNumber});
        //验证码模板ID
        req.setTemplateID(config.getTencentSmsVerificationCodeTemplateID());// TODO 需要修改到配置文件
        //审核通过的签名
        req.setSign(config.getTencentSmsSign());// TODO 需要修改到配置文件
        req.setTemplateParamSet(new String[]{typeName});
        //短信应用ID
        req.setSmsSdkAppid(config.getTencentSmsSdkAppid());//TODO 需要修改到配置文件
        SendSmsResponse resp = client.SendSms(req);
        System.out.println(SendSmsRequest.toJsonString(resp));
        return resp.getSendStatusSet()[0];
    }

    public static void main(String [] args) {
        try{
            SmsUtilsByTencent.sendSmsVerificationCode("18116590818", "9799999");

//            Credential cred = new Credential("AKIDPcYEfpwPDl3QWHpyw0wbzOHZdXAZnIOE", "UjyzkGiuynMgjW2UKvGF0Vu3QXLuDI7g");
//
//            HttpProfile httpProfile = new HttpProfile();
//            httpProfile.setEndpoint("sms.tencentcloudapi.com");
//
//            ClientProfile clientProfile = new ClientProfile();
//            clientProfile.setHttpProfile(httpProfile);
//
//            SmsClient client = new SmsClient(cred, "", clientProfile);
//
//            //String signStr = new String("智达科创".getBytes("gbk"),"utf-8");
//            String params = "{\"PhoneNumberSet\":[\"+8618116590818\"],\"TemplateID\":\"611770\",\"Sign\":\"智达科创\",\"TemplateParamSet\":[\"666666\"],\"SmsSdkAppid\":\"1400373413\"}";
//            SendSmsRequest req = SendSmsRequest.fromJsonString(params, SendSmsRequest.class);
//            //String[] pars = {"88888888"};
//            //req.setTemplateParamSet(pars);
//
//            SendSmsResponse resp = client.SendSms(req);
//
//            System.out.println(SendSmsRequest.toJsonString(resp));
        } catch (TencentCloudSDKException e) {
            System.out.println(e.toString());
        }

    }

    /**
     * 办件受理
     * @param phoneNumber 国内手机号(此方法会在号码前添加+86)
     * @param typeName 业务名称
     * @return SendStatus.getCode()=="Ok"，表示发送成功
     * @throws TencentCloudSDKException
     */
    public static SendStatus SendResultSL(String phoneNumber, String typeName,String content) throws TencentCloudSDKException {
        SysConfig config = SpringContextHolder.getBean(SysConfigService.class).get("6");
        // TODO 需要修改到配置文件
        Credential cred = new Credential(config.getTencentApiSecretId(), config.getTencentApiSecretKey());

        HttpProfile httpProfile = new HttpProfile();
        httpProfile.setEndpoint("sms.tencentcloudapi.com");

        ClientProfile clientProfile = new ClientProfile();
        clientProfile.setHttpProfile(httpProfile);
        SmsClient client = new SmsClient(cred, "", clientProfile);
        SendSmsRequest req = new SendSmsRequest();
        req.setPhoneNumberSet(new String[]{"+86"+phoneNumber});
        //验证码模板ID
        req.setTemplateID(config.getTencentSmsVerificationCodeTemplateID());// TODO 需要修改到配置文件
        //审核通过的签名
        req.setSign(config.getTencentSmsSign());// TODO 需要修改到配置文件
        req.setTemplateParamSet(new String[]{typeName,content});
        //短信应用ID
        req.setSmsSdkAppid(config.getTencentSmsSdkAppid());//TODO 需要修改到配置文件
        SendSmsResponse resp = client.SendSms(req);
        System.out.println(SendSmsRequest.toJsonString(resp));
        return resp.getSendStatusSet()[0];
    }


}
